<?php
/**
 * 活动的实物投放
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 11:26
 */
namespace App;

use Endroid\QrCode\Factory\QrCodeFactory;
use Endroid\QrCode\QrCode;
use Model\ActivityMemberMod;
use Model\ActivityMemberTreasureMod;
use Model\ActivityMod;
use Model\ActivityTreasureMod;

class Entity extends Action
{
	private $ActiityTreasure = null;  //活动投放的奖励
	private $MemberTreasure = null;  //用户开到的奖励
	private $ActivityMember = null;  //活动的会员列表
	private $Activity = null;  //活动

	public function __construct()
	{
		parent::__construct();
		$this->ActiityTreasure = new ActivityTreasureMod();
	}

	/**
	 * 实物奖励列表
	 */
	public function li()
	{
		$_list = $this->ActiityTreasure->findAll(array('activity_id'=>$this->_gpc['activity_id'],'type'=>2,'status'=>1),array('order'=>array('id','asc')),array('id','name','icon','money','number','win_odds','user_number'));
		if(count($_list) > 0){
			foreach($_list as $_k=>$_v){
				$_list[$_k]['icon_url'] = tomedia($_v['icon']);
			}
			$this->_result = array('status'=>'success','data'=>$_list);
		}
		$this->result($this->_result);
	}

	/**
	 * 添加实物奖励
	 */
	public function add()
	{
		if($this->_gpc['data']['win_odds'] == 1){
			if($this->ActiityTreasure->findOne(array('activity_id'=>$this->_gpc['data']['activity_id'],'type'=>2,'win_odds'=>1,'status'=>1),array('id'))){
				$this->_result = array('status'=>'error','err_status'=>9,'err_msg'=>'每个活动中奖率为1的实物只能投放1个');
				$this->result($this->_result);
			}
		}
		$_result = $this->ActiityTreasure->insert($this->_gpc['data']);
		if($_result){
			$this->_result = array('status'=>'success');
		}else{
			$this->_result = array('status'=>'error','err_status'=>8,'err_msg'=>'添加失败');
		}
		$this->result($this->_result);
	}

	/**
	 * 修改实物奖励
	 */
	public function edit()
	{
		if($this->_gpc['data']['win_odds'] == 1){
			if($this->ActiityTreasure->findOne(array('activity_id'=>$this->_gpc['data']['activity_id'],'type'=>2,'win_odds'=>1,'status'=>1,'id !='=>$this->_gpc['entity_id']),array('id'))){
				$this->_result = array('status'=>'error','err_status'=>9,'err_msg'=>'每个活动中奖率为1的实物只能投放1个');
				$this->result($this->_result);
			}
		}
		$_result = $this->ActiityTreasure->update($this->_gpc['data'],array('id'=>$this->_gpc['entity_id']));
		if($_result){
			$this->_result = array('status'=>'success');
		}else{
			$this->_result = array('status'=>'error','err_status'=>8,'err_msg'=>'修改失败');
		}
		$this->result($this->_result);
	}

	/**
	 * 删除实物
	 */
	public function delete(){
		if($this->ActiityTreasure->update(array('status'=>0),array('id'=>$this->_gpc['entity_id']))){
			$this->_result = array('status'=>'success');
		}else{
			$this->_result = array('status'=>'error','err_status'=>9,'err_msg'=>'删除失败');
		}
		$this->result($this->_result);
	}

	/**
	 * 设置实物兑换地址
	 */
	public function setEntityAddress()
	{
		$this->Activity = new ActivityMod();
		if($this->Activity->update(array('entity_address'=>$this->_gpc['entity_address']),array('id'=>$this->_gpc['activity_id']))){
			$this->_result = array('status'=>'success');
		}
		$this->result($this->_result);
	}

	/**
	 * 某个用户的实物奖品列表
	 */
	public function exchangeList()
	{
		$this->MemberTreasure = new ActivityMemberTreasureMod();
		$_where = array('activity_id'=>$this->_gpc['activity_id'],'uid'=>$this->_gpc['uid'],'type'=>2);
		if(isset($this->_gpc['exchange'])) $_where['exchange'] = $this->_gpc['exchange'];
		$_list = $this->MemberTreasure->findAll($_where,array('order'=>array('exchange','asc','id','asc')),array('treasure_id','number','exchange'));
		if(count($_list) > 0){
			foreach($_list as $_k=>$_v){
				$_treasure = $this->ActiityTreasure->findOne(array('id'=>$_v['treasure_id']),array('name','icon','money'));
				$_list[$_k]['name'] = $_treasure['name'];
				$_list[$_k]['money'] = $_treasure['money'];
				$_list[$_k]['icon'] = tomedia($_treasure['icon']);
			}
			$this->_result = array('status'=>'success','data'=>$_list);
		}
		$this->result($this->_result);
	}

	/**
	 * 获取活动的实物兑换列表
	 */
	public function exchangeLi()
	{
		if(isset($this->_gpc['uid'])){
			$_uid = $this->_gpc['uid'];
		}else{
			$_uid = 0;
		}
		$this->MemberTreasure = new ActivityMemberTreasureMod();
		$_list = $this->MemberTreasure->entityList($this->_gpc['activity_id'],$this->_gpc['status'],$this->_gpc['limit'],$_uid);
		if(count($_list) > 0){
			$this->ActivityMember = new ActivityMemberMod();
			foreach($_list as $_k=>$_v){
				$_member = $this->uidToInfo($_v['uid']);
				$_list[$_k]['header'] = $_member['avatar'];
				$_list[$_k]['nickname'] = $_member['nickname'];
			}
			$this->_result = array('status'=>'success','data'=>$_list);
		}
		$this->result($this->_result);
	}

	/**
	 * 获取活动兑换列表总数
	 */
	public function getTotal()
	{
		if(isset($this->_gpc['uid'])){
			$_uid = $this->_gpc['uid'];
		}else{
			$_uid = 0;
		}
		$this->MemberTreasure = new ActivityMemberTreasureMod();
		$_total = $this->MemberTreasure->entityListTotal($this->_gpc['activity_id'],$this->_gpc['status'],$_uid);
		$this->_result = array('status'=>'success','data'=>$_total);
		$this->result($this->_result);
	}

	/**
	 * 活动是打开了实物投放
	 */
	public function entityOpen()
	{
		$_treasure = $this->ActiityTreasure->findOne(array('activity_id'=>$this->_gpc['activity_id'],'type'=>2,'status'=>1));
		if(!$_treasure){
			$this->_result = array('status'=>'error');
			$this->result($this->_result);
		}else{
			$this->_result = array('status'=>'success');
			$this->result($this->_result);
		}
	}

	/**
	 * 获取地址
	 */
	public function getAddress()
	{
		$Activity = new ActivityMod();
		$_activity = $Activity->findOne(array('id'=>$this->_gpc['activity_id']),array('entity_address'));
		if($_activity) $this->_result = array('status'=>'success','data'=>$_activity['entity_address']);
		$this->result($this->_result);
	}

	/**
	 * 前端获取用户兑换码
	 */
	public function exchangeNumber()
	{
		$this->ActivityMember = new ActivityMemberMod();
		$_member = $this->ActivityMember->findOne(array('activity_id'=>$this->_gpc['activity_id'],'uid'=>$this->_gpc['uid']),array('exchange_number'));
		if($_member){
            $_url = murl('entry//iscan',array('m'=>'kuyuan_vbz','type'=>'exchange','activity_id'=>$this->_gpc['activity_id'],'uid'=>$this->_gpc['uid'],'number'=>$_member['exchange_number']),true,true);
		    $_qrcode = $this->getQrcode('exchange_qrcode',$this->_gpc['activity_id'].'_'.$this->_gpc['uid'],$_url,true);
		    $_result = array();
		    if($_qrcode) $_result = array('exchange_number'=>$_member['exchange_number'],'qrcode'=>$_qrcode);
            $this->_result = array('status'=>'success','data'=>$_result);
        }
		$this->result($this->_result);
	}

	/**
	 * 后台兑换核销实物
	 */
	public function exchangeAfter()
	{
		$this->ActivityMember = new ActivityMemberMod();
		$_member = $this->ActivityMember->findOne(array('uid'=>$this->_gpc['uid'],'activity_id'=>$this->_gpc['activity_id'],'exchange_number'=>$this->_gpc['exchange_number']),array('id'));
		if($_member){
			$this->MemberTreasure = new ActivityMemberTreasureMod();
			$_member_treasure = $this->MemberTreasure->findAll(array('activity_id'=>$this->_gpc['activity_id'],'uid'=>$this->_gpc['uid'],'type'=>2,'exchange'=>0),array('id'));
			foreach($_member_treasure as $_k=>$_v){
				$this->MemberTreasure->update(array('exchange'=>1),array('id'=>$_v['id']));
			}
			$this->ActivityMember->update(array('exchange_number'=>mt_rand(100000,999999)),array('id'=>$_member['id']));
			$this->_result = array('status'=>'success');
		}else{
			$this->_result = array('status'=>'error');
		}
		$this->result($this->_result);
	}

}